package com.esunny.resourcevisualization.controller;

import com.esunny.resourcevisualization.domain.entity.UserEntity;
import com.esunny.resourcevisualization.domain.response.ReturnStructure;
import com.esunny.resourcevisualization.service.UserEntityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @version 1.0.0.0
 * @ClassName UserEntityController
 * @Description 用户-实体对应接口
 * @Author baochencong
 * @Date 2020/11/5 10:37
 */
@Controller
@RequestMapping("/resource")
public class UserEntityController {

    @Resource
    private UserEntityService userEntityService;

    /**
     * 通过实体id获取用户-实体对应关系
     *
     * @param entityId 实体id
     * @return 用户-实体对应关系信息
     */
    @ResponseBody
    @GetMapping("/getUserEntityByEntityId/{entityId}")
    public ReturnStructure getUserEntityByEntityId(@PathVariable("entityId") int entityId) {
        if (StringUtils.isBlank(entityId + "")) {
            return ReturnStructure.NOT_FOUND();
        }
        return ReturnStructure.SUCCESS("获取用户-实体对应关系成功", userEntityService.getUserEntityByEntityId(entityId));
    }

    /**
     * 通过实体id删除用户-实体对应关系
     *
     * @param entityId 实体id
     * @return 删除用户-实体对应关系结果
     */
    @ResponseBody
    @PostMapping("/deleteUserEntityByEntityId")
    public ReturnStructure deleteUserEntityByEntityId(@RequestParam("entityId") int entityId) {
        if (StringUtils.isBlank(entityId + "")) {
            return ReturnStructure.NOT_FOUND();
        }
        return ReturnStructure.SUCCESS("删除用户-实体对应关系成功", userEntityService.deleteUserEntityByEntityId(entityId));
    }

    /**
     * 添加用户-实体对应关系
     *
     * @param userEntity 用户-实体对应关系
     * @return 添加用户-实体对应关系结果
     */
    @ResponseBody
    @PostMapping("/addUserEntity")
    public ReturnStructure addUserEntity(@RequestBody UserEntity userEntity) {
        if (userEntity == null) {
            return ReturnStructure.NOT_FOUND();
        }
        return ReturnStructure.SUCCESS("添加用户-实体对应关系成功", userEntityService.addUserEntity(userEntity));
    }

    /**
     * 通过实体id更新用户-实体对应关系
     *
     * @param userEntity 用户-实体对应关系
     * @return 更新用户-实体1对应关系结果
     */
    @ResponseBody
    @PostMapping("/updateUserEntityByEntityId")
    public ReturnStructure updateUserEntityByEntityId(@RequestBody UserEntity userEntity) {
        if (userEntity == null) {
            return ReturnStructure.NOT_FOUND();
        }
        return ReturnStructure.SUCCESS("修改用户-实体对应关系成功", userEntityService.updateUserEntityByEntityId(userEntity));
    }

}
